// 启动 npm nodemon xxx.js  自动运行修改的文件
// Generator 生成器

function* foo() {
  console.log('foo')
  yield 100
  yield 200
  // return 100
  return 300
}

const result = foo()
// console.log(result.next())
// console.log(result.next())
// console.log(result.next())
// console.log(result.next())

//  案例1：发号器
function* createId() {
  let id = 1
  while (true) {
    yield id++
  }
}
const idmack = createId()
console.log(idmack.next())
console.log(idmack.next())
console.log(idmack.next())
console.log(idmack.next())
console.log(idmack.next())
console.log(idmack.next())
console.log(idmack.next())
console.log(idmack.next())

// 案例2 使用Generator 函数实现 iterator 方法
const todos = {
  a: ['吃饭', '睡觉', '打豆豆'],
  b: ['语文', '数学', '英语'],
  c: ['电视', '冰箱'],
  [Symbol.iterator]: function* () {
    let index = 0
    const self = this
    const arr = [].concat(this.a, this.b, this.c)
    for (const key of arr) {
      yield key
    }
    // return {
    //   next: function () {
    //     const value = {
    //       value: arr[index],
    //       done: index++ >= arr.length
    //     }
    //     return value
    //   }
    // }
  },
}

for (const key of todos) {
  console.log(key)
}

const arr = [1, 2, 3, 45, 5]
